using GalaSoft.MvvmLight;
using Microsoft.SharePoint;
using System;
using SharePoint.Utility.Logging;
using CopyOrdersList.BL;
using GalaSoft.MvvmLight.CommandWpf;
using System.Windows.Input;
using System.Collections.ObjectModel;
using CopyOrdersList.BL.Helpers;
using UIControlWPF.Model;
using NLog;

namespace UIControlWPF.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        ILogger logger = DetailLog.GetLogger;
        public MainViewModel()
        {
            CurrentSPModel = new SPModel();
            LogsList = new ObservableCollection<SPLogs>();
            StartRestoreCommand = new RelayCommand<SPModel>(StartRestoreMethod);
            RenameListCommand = new RelayCommand(RenameListMethod);
        }

        private SPModel selectedSPModel;

        ObservableCollection<SPLogs> _SPLogs;

        public ObservableCollection<SPLogs> LogsList
        {
            get { return _SPLogs; }
            set
            {
                _SPLogs = value;
                RaisePropertyChanged("LogsList");
            }
        }

        public void AddTrace(string message, string status, string place) {
            LogsList.Add(new SPLogs(message, status, place));
            RaisePropertyChanged("LogsList");
        }

        public void AddTrace(string message)
        {
            LogsList.Add(new SPLogs(message, "Trace", ""));
            RaisePropertyChanged("LogsList");
        }


        public ICommand StartRestoreCommand { get; private set; }
        public ICommand RenameListCommand { get; private set; }


        public SPModel CurrentSPModel
        {
            get
            {
                return selectedSPModel;
            }
            set
            {
                selectedSPModel = value;
                RaisePropertyChanged("SelectedSPModel");
            }
        }


        private void RenameListMethod()
        {
            throw new NotImplementedException();
        }



        void StartRestoreMethod(SPModel model)
        {
            AddTrace("Start restore");
            SPSecurity.RunWithElevatedPrivileges(delegate ()
            {
                try
                {
                    using (var site = new SPSite(model.SiteUrl))
                    {
                        using (var web = site.OpenWeb("/" + model.Web))
                        {
                            SPList orders = ListHelper.getListByUrl(web, model.List);
                            try
                            {
                                RestoreInstance restore = new RestoreInstance(web);
                                restore.RestoreAsNewList(orders);
                            }
                            catch (Exception ex)
                            {
                                logger.Fatal(ex.ToString());
                                AddTrace(ex.ToString(), "Fatal", "class: RestoreInstance");
                            }
                        }
                    }                   
                }
                catch (Exception ex)
                {
                    logger.Fatal(ex.ToString());
                    AddTrace(ex.ToString(), "Fatal", "Open Site");
                }
            });
            AddTrace("Restoreing has finished");
        }

    }
}